package com.henu.ws.rest;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.smart4j.framework.ioc.annotation.Inject;
import org.smart4j.framework.tx.annotation.Service;

import com.henu.service.CartService;
import com.henu.service.OrderInfoService;
import com.henu.service.UserLoginService;
import com.smart.plugin.ws.WebService;

@Service
@WebService(value = "/User", type = WebService.Type.REST)
@Produces(MediaType.APPLICATION_JSON)
public class UserLoginWS {
    @Inject
    private UserLoginService userLoginService;

    @Inject
    private CartService cartService;

    @Inject
    private OrderInfoService orderInfoService;

    @POST
    @Path("/UserLogin")
    public Map checkLogin(@FormParam("userName") String username, @FormParam("password") String password) {
        Map m = new HashMap();
        m.put("UserLoginResult", userLoginService.checkUserLogin(username, password));
        return m;
    }
    
    @POST
    @Path("/AddProductToCart")
    public Map addProductOToCart(@FormParam("userID") Integer userID, @FormParam("productID") Integer productID) {
        Map result=new HashMap<>();
        boolean addProductToCartResult= false;
        if (userID != 0 && productID != 0) {
            addProductToCartResult= cartService.addCart(userID, productID);
        }
        result.put("AddProductToCartResult", addProductToCartResult);
        return result;
    }

    @POST
    @Path("/CreateOrder")
    public Map CreateOrder(@FormParam("userID") Integer userID, @FormParam("name") String name,
            @FormParam("address") String address, @FormParam("phone") String phone, @FormParam("zcode") String zcode,
            @FormParam("productIDList") String productIDList) {
        Map result = new HashMap<>();
        boolean createOrderResult = false;
        if (userID != 0) {
            createOrderResult = orderInfoService.createOrder(userID, name, address, zcode, phone, productIDList);
            createOrderResult = cartService.delCart(userID) && createOrderResult;
        }
        result.put("createOrderResult", createOrderResult);
        return result;
    }

    @POST
    @Path("/GetCart")
    public Map getCart(@FormParam("userID") Integer userID) {
        Map result = new HashMap<>();
        result.put("getCartResult", cartService.queryCartByUserID(userID));
        return result;
    }

    @POST
    @Path("/GetNewOrder")
    public Map getNewOrder(@FormParam("userID") Integer userID) {
        Map res = new HashMap<>();
        res.put("getNewOrderResult", orderInfoService.getNewOrder(userID));
        return res;
    }
    
    @POST
    @Path("/GetHistoryOrder")
    public Map getHistoryOrder(@FormParam("userID") Integer userID) {
        Map res = new HashMap<>();
        res.put("getHistoryOrderResult", orderInfoService.getHistoryOrder(userID));
        return res;
    }
    
    
    @POST
    @Path("/GetSendProduct")
    public Map getSendProduct(@FormParam("productID") Integer productID, @FormParam("orderID") Integer orderID
            ) {
        Map res = new HashMap<>();
        res.put("isSuccess", orderInfoService.sendProduct(productID, orderID));
        return res;
    }
    @POST
    @Path("/ProductAppraise")
    public Map productAppraise(@FormParam("productID") Integer productID, @FormParam("orderID") Integer orderID,
            @FormParam("userID") Integer userID,
            @FormParam("star") float star, @FormParam("comment") String comment) {
        Map res = new HashMap<>();
        res.put("ProductAppraiseResult", orderInfoService.productAppraise(orderID, productID, userID, star, comment));
        return res;
    }
}
